<?php

/**
 * search actions.
 *
 * @package    sf_sandbox
 * @subpackage search
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class searchActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request) {




		/*
		 * Признак расширенного поиска
		 */
		if($request->getParameter('adv')=='true')
		{
			$this->adv = true;
		}
		else
		{
			$this->adv = false;
		}


		/*
		 * Постраничка
		 */
	    if(!$request->getParameter('page'))
  	    {
  	    	$this->page =1;
  	    }
  	    else
  	    {
  	    	$this->page=$request->getParameter('page');
  	    }

            if(!$this->getUser()->isAuthenticated() and ($this->page%2==0))
                    {
                      $this->getUser ()->setFlash ( 'message', __('Register please') );
                      $this->redirect('@registration');
                    }


            $c = $this->buildCriteriasearch($request);


		///
		/*
		 * Сортировка
		 */
		 if($request->getParameter('sort'))
		 {
		 	$this->order = $request->getParameter('sort');
		 }
		 else
		 {
		 	$this->order = 2;
		 }
		switch ($this->order) {
			case 1 :
				$this->setSort ( $c, $this->order, sfGuardUserPeer::NICKNAME );
				break; // рейтинг
			case 2 :
				$this->setSort ( $c, $this->order, sfGuardUserPeer::IS_ONLINE );
				//$c->add(sfGuardUserPeer::IS_ONLINE,true);
				break; // оценка
			case 3 :
				$this->setSort ( $c, $this->order, sfGuardUserPeer::CREATED_AT );
				break;

			 default :
               $this->setSort ( $c, $this->order, sfGuardUserPeer::NICKNAME );
				break;

		     }
		     
 if($this->getUser()->isAuthenticated())
         {
              if($this->getUser()->isSuperAdmin())
              {

              }
              elseif($this->getUser()->getProfile()->getGender()=='m')
              {
                 $c->add(sfGuardUserPeer::GENDER,'w');
              }
              elseif($this->getUser()->getProfile()->getGender()=='w')
              {
                 $c->add(sfGuardUserPeer::GENDER,'m');
              }
         }

		$c->addDescendingOrderByColumn(sfGuardUserPeer::ID);
		$c->add(sfGuardUserPeer::IS_PARTNER,false);
		$pager = new sfPropelPager ( 'sfGuardUser', 12 );
		$pager->setCriteria ( $c );
		$pager->setPeerMethod ( 'doSelect' );
		$pager->setPage ( $this->page );
		$pager->init ();
		$this->user = $pager;

              $this->setLayout('alika_layout');
	}





	public function executeAdvform(sfWebRequest $request) {
		$this->form = new AdvForm();

	}








	public function setSort(&$c, $order, $field) {
		if ($order == 'asc')
			$c->addAscendingOrderByColumn ( $field );
		else
			$c->addDescendingOrderByColumn ( $field );
	}

         public function executeForid(sfWebRequest $request) {

             $this->setDecorator();
             $user = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
             if($user) $this->redirect('profile/show?id='.$user->getId());

         }



        public function buildCriteriasearch($request) {

                $c = new Criteria ( );
		$c->add ( sfGuardUserPeer::IS_OTPUSK, false );
		$c->add ( sfGuardUserPeer::IS_ACTIVE, true );
                $c->add(sfGuardUserPeer::IS_PARTNER,false);
                $c->add(sfGuardUserPeer::IS_SUPER_ADMIN,false);

               // $crit1=$c->getNewCriterion(sfGuardUserPeer::GENDER, 'm');
              //  $crit2=$c->getNewCriterion(sfGuardUserPeer::IS_ONLINE,true);
              //  $crit1->addAnd($crit2);


		/*
                  *  Поиск по ID
                  */
		if ($request->getParameter ( 'key' ) != '') {
			$c->add ( sfGuardUserPeer::ID, $request->getParameter ( 'key' ) );
		}

       /*
      *  Поиск по Nickname
      */
		if ($request->getParameter ( 'nick' ) != '') {
			$c->add ( sfGuardUserPeer::NICKNAME, '%'.$request->getParameter ( 'nick' ).'%', Criteria::LIKE );
		}

		/*
      *  Поиск по полу
      */
		if ($request->getParameter ( 'gender' ) != '') {
			$c->add ( sfGuardUserPeer::GENDER, $request->getParameter ( 'gender' ) );
                        //if($request->getParameter ( 'gender' )=='m')
                         //   {
                          //      $c->add ( sfGuardUserPeer::IS_ONLINE, true );
                          //  }
		}

	  /*
      *  Поиск по соц статусу
      */
		if ($request->getParameter ( 'is_marital' ) != '') {
			$c->add ( sfGuardUserPeer::IS_MARITAL, $request->getParameter ( 'is_marital' ) );
		}

		  /*
      *  Дети у партнера
      */
		if ($request->getParameter ( 'have_child_partner' )  == '1') {
			$c->add ( sfGuardUserPeer::HAVE_CHILD_PARTNER, $request->getParameter ( 'have_child_partner' ) );
		}

		  /*
      *  Дети у юзера
      */
		if ($request->getParameter ( 'have_child' )  == '1') {
			$c->add ( sfGuardUserPeer::HAVE_CHILD, $request->getParameter ( 'have_child' ) );
		}

		  /*
      *  Цвет глаз
      */
		if ($request->getParameter ( 'eye_color' ) != '') {
			$c->add ( sfGuardUserPeer::EYE_COLOR, $request->getParameter ( 'eye_color' ) );
		}

	  /*

	 /*
      *  Длинна волос
      */
		if ($request->getParameter ( 'hair_lenght' ) != '') {
			$c->add ( sfGuardUserPeer::HAIR_LENGHT, $request->getParameter ( 'hair_lenght' ) );
		}

	  /*

	 /*
      *  Цвет волос
      */
		if ($request->getParameter ( 'hair_color' ) != '') {
			$c->add ( sfGuardUserPeer::HAIR_COLOR, $request->getParameter ( 'hair_color' ) );
		}

	  /*

	 /*
      *  Тип волос
      */
		if ($request->getParameter ( 'hair_type' ) != '') {
			$c->add ( sfGuardUserPeer::HAIR_TYPE, $request->getParameter ( 'hair_type' ) );
		}






        ////  Поиск кто с фотками

		if ($request->getParameter ( 'widt_photo' ) != '') {
			$c->add ( sfGuardUserPeer::HAVE_PHOTOS, true );
		}



       //  Поиск кто с видео

		if ($request->getParameter ( 'width_video' ) != '') {
			$c->add ( sfGuardUserPeer::HAVE_VIDEO, true );
		}

       //  Поиск кто в онлайн

		if ($request->getParameter ( 'is_online' ) != '') {
			$c->add ( sfGuardUserPeer::IS_ONLINE, true );
		}


          //  Поиск по стране

		if ($request->getParameter ( 'country' ) != '') {
			$c->add ( sfGuardUserPeer::COUNTRY, $request->getParameter ( 'country' ) );
		}

             ////  Поиск кто с детьми

		if ($request->getParameter ( 'have_child' ) != '') {
			$c->add ( sfGuardUserPeer::HAVE_CHILD, $request->getParameter ( 'have_child' ) );
		}

             ////  Поиск кто курит

		if ($request->getParameter ( 'smoking' ) != '') {
			$c->add ( sfGuardUserPeer::SMOKING, $request->getParameter ( 'smoking' ) );
		}

             ////  Поиск кто пьет

		if ($request->getParameter ( 'drinking' ) != '') {
			$c->add ( sfGuardUserPeer::DRINKING, $request->getParameter ( 'drinking' ) );
		}

             ////  Поиск по религии

		if ($request->getParameter ( 'religion' ) != '') {
			$c->add ( sfGuardUserPeer::RELIGION, $request->getParameter ( 'religion' ) );
		}

		if ($request->getParameter ( 'is_new' ) == '1') {
			$c->add ( sfGuardUserPeer::CREATED_AT, '2009-10-01', Criteria::GREATER_THAN );
		}


             ////  Поиск по зодиаку

		if (is_array($request->getParameter ( 'zodiac' ))) {
			$crit1=$c->getNewCriterion(sfGuardUserPeer::USERNAME,'tr');
                        foreach($request->getParameter ( 'zodiac' ) as $k=>$v)
                            {
                                $crit2=$c->getNewCriterion(sfGuardUserPeer::ZODIAC,$v);

                                $crit1->addOr($crit2);
                            }
                        $c->add($crit1);
		}


                 ////  Поиск на знание английского языка

		if ($request->getParameter ( 'engl' ) == 1) {
			$c->addJoin( sfGuardUserPeer::ID, User2langPeer::USER_ID );
                        $c->add(User2langPeer::LANG,'en');
		}

		//$dt = '1977-05-23';
		$dt1 = new sfDate ( );
		$dt2 = new sfDate ( );

		////Возраст


		if ($request->getParameter ( 'age_from' ) > 1 and $request->getParameter ( 'age_to' ) == 0) {
			$c->add ( sfGuardUserPeer::BIRTHDAY, $dt1->subtractYear ( $request->getParameter ( 'age_from' ) )->format ( 'Y-m-d' ), Criteria::LESS_THAN );

		}

		if ($request->getParameter ( 'age_to' ) > 1 and $request->getParameter ( 'age_from' ) == 0) {
			$c->add ( sfGuardUserPeer::BIRTHDAY, $dt1->subtractYear ( $request->getParameter ( 'age_to' ) )->format ( 'Y-m-d' ), Criteria::GREATER_THAN );

		}

		if ($request->getParameter ( 'age_to' ) > 1 and $request->getParameter ( 'age_from' ) > 1) {

			$crit1 = $c->getNewCriterion ( sfGuardUserPeer::BIRTHDAY, $dt1->subtractYear ( $request->getParameter ( 'age_from' ) )->format ( 'Y-m-d' ), Criteria::LESS_THAN );
			$crit2 = $c->getNewCriterion ( sfGuardUserPeer::BIRTHDAY, $dt2->subtractYear ( $request->getParameter ( 'age_to' ) )->format ( 'Y-m-d' ), Criteria::GREATER_THAN );
			$crit1->addAnd ( $crit2 );
			$c->add ( $crit1 );

		}



		///


		////Рост

		if ($request->getParameter ( 'height_from' ) > 1 and $request->getParameter ( 'height_to' ) == 0) {
			$c->add ( sfGuardUserI18nPeer::HEIGHT, $request->getParameter ( 'height_from' ), Criteria::LESS_THAN );

		}

		if ($request->getParameter ( 'height_to' ) > 1 and $request->getParameter ( 'height_from' ) == 0) {
			$c->add ( sfGuardUserI18nPeer::HEIGHT, $request->getParameter ( 'height_to' ), Criteria::GREATER_THAN );

		}

		if ($request->getParameter ( 'height_to' ) > 1 and $request->getParameter ( 'height_from' ) > 1) {
			$crit1 = $c->getNewCriterion ( sfGuardUserPeer::HEIGHT, $request->getParameter ( 'height_to' ), Criteria::LESS_THAN );
			$crit2 = $c->getNewCriterion ( sfGuardUserPeer::HEIGHT, $request->getParameter ( 'height_from' ), Criteria::GREATER_THAN );
			$crit1->addAnd ( $crit2 );
			$c->add ( $crit1 );

		}

                 ////Вес

		if ($request->getParameter ( 'ves_from' ) > 1 and $request->getParameter ( 'ves_to' ) == 0) {
			$c->add ( sfGuardUserI18nPeer::HEIGHT, $request->getParameter ( 'ves_from' ), Criteria::LESS_THAN );

		}

		if ($request->getParameter ( 'ves_to' ) > 1 and $request->getParameter ( 'ves_from' ) == 0) {
			$c->add ( sfGuardUserI18nPeer::HEIGHT, $request->getParameter ( 'ves_to' ), Criteria::GREATER_THAN );

		}

		if ($request->getParameter ( 'ves_to' ) > 1 and $request->getParameter ( 'ves_from' ) > 1) {
			$crit1 = $c->getNewCriterion ( sfGuardUserPeer::HEIGHT, $request->getParameter ( 'ves_to' ), Criteria::LESS_THAN );
			$crit2 = $c->getNewCriterion ( sfGuardUserPeer::HEIGHT, $request->getParameter ( 'ves_from' ), Criteria::GREATER_THAN );
			$crit1->addAnd ( $crit2 );
			$c->add ( $crit1 );

		}

               // $crit1=$c->getNewCriterion(sfGuardUserPeer::GENDER, 'm');
              //  $crit2=$c->getNewCriterion(sfGuardUserPeer::IS_ONLINE,true);
              //  $crit1->addAnd($crit2);
              //  $c->addOr($crit1);
                

                return $c;


	}


}
